/*
 * =====================================================================================
 *
 *       Filename:  core_match.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2025年08月19日 17时29分42秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>

#define NUM_CORES 12
#define NUM_SLICES 24

// 时延数据表（行：核心编号，列：切片编号）
// 注意：图中切片编号是0-23，核心编号是0-11
int latency_table[NUM_CORES][NUM_SLICES] = {
    // 核心0的时延数据（切片0-23）
    {85,    76, 80, 89, 87, 80, 89, 98, 92, 90, 92, 98, 104,    98, 98, 99, 104,    110,    108,    107,    108,    116,    110,    111},
    // 核心1的时延数据（切片0-23）
    {109,   105,    99, 103,    103,    97, 92, 99, 91, 91, 86, 85, 92, 89, 84, 80, 79, 100,    97, 94, 87, 102,    97, 94},
    // 核心2的时延数据（切片0-23）
    {115,   109,    108,    110,    110,    107,    107,    103,    99, 100,    96, 98, 99, 96, 89, 88, 88, 88, 85, 82, 81, 92, 88, 82},
    // 核心3的时延数据（切片0-23）
    {93,    87, 82, 92, 88, 84, 79, 100,    98, 96, 92, 89, 103,    103,    99, 95, 94, 116,    110,    110,    105,    116,    110,    107},
    // 核心4的时延数据（切片0-23）
    {102,   95, 91, 98, 94, 94, 94, 90, 86, 83, 79, 84, 92, 92, 90, 83, 88, 104,    100,    100,    96, 104,    102,    98},
    // 核心5的时延数据（切片0-23）
    {115,   106,    111,    110,    107,    104,    109,    104,    98, 96, 98, 103,    97, 93, 92, 92, 97, 89, 87, 82, 86, 86, 80, 81},
    // 核心6的时延数据（切片0-23）
    {88,    87, 87, 82, 80, 82, 88, 91, 86, 90, 95, 98, 98, 95, 97, 97, 104,    107,    102,    107,    110,    110,    110,    110},
    // 核心7的时延数据（切片0-23)
    {96,    91, 94, 95, 89, 89, 93, 86, 80, 78, 81, 86, 92, 86, 85, 87, 90, 98, 97, 96, 95, 104,    98, 98},
    // 核心8的时延数据（切片0-23）
    {100,   100,    104,    97, 95, 97, 104,    89, 84, 89, 93, 91, 82, 78, 82, 85, 88, 93, 90, 95, 95, 93, 95, 97},
    // 核心9的时延数据（切片0-23）
    {105,   109,    116,    102,    104,    109,    115,    91, 96, 97, 103,    103,    89, 92, 94, 97, 100,    79, 82, 85, 88, 82, 88, 90},
    // 核心10的时延数据（切片0-23）
    {108,   116,    116,    103,    107,    110,    115,    98, 99, 103,    103,    110,    91, 93, 97, 100,    103,    83, 87, 88, 91, 80, 85, 88},
    // 核心11的时延数据（切片0-23）
    {89,    96, 98, 87, 88, 93, 98, 79, 80, 84, 88, 90, 85, 86, 91, 91, 97, 91, 95, 97, 102,    98, 102,    102},
};

// 核心匹配函数
int match_core(int slice1, int slice2, bool find_min) {
    int best_core = -1;
    int best_sum = find_min ? INT_MAX : INT_MIN;

    for (int core = 0; core < NUM_CORES; core++) {
        // 获取当前核心访问两个切片的时延
        int latency1 = latency_table[core][slice1];
        int latency2 = latency_table[core][slice2];
        int sum = latency1 + latency2;

        // 根据bool参数选择最小或最大的时延和
        if (find_min) {
            if (sum < best_sum) {
                best_sum = sum;
                best_core = core;
            }
        } else {
            if (sum > best_sum) {
                best_sum = sum;
                best_core = core;
            }
        }
    }

    return best_core;
}

    
void check_min(){
    for (int s1=0; s1<NUM_SLICES; s1++){
        for (int s2=s1+1; s2<NUM_SLICES; s2++){
            int sum_min = INT_MAX;
            int core_min = -1;
            for (int core=0; core<NUM_CORES; core++){
                int sum = latency_table[core][s1] + latency_table[core][s2];
                if (sum < sum_min){
                    sum_min = sum;
                    core_min = core;
                }
            }
           // printf("core %2d,  slices %2d %2d,  sum_min %3d\n", core_min, s1, s2, sum_min);
            for (int core=0; core<NUM_CORES; core++){
                int sum = latency_table[core][s1] + latency_table[core][s2];
                if (sum == sum_min && core != core_min){
                    printf("======> core %2d %2d,  slices %2d %2d,  sum_min %3d\n", core, core_min, s1, s2, sum_min);
                }
            }
        }
    }
}

void check_max(){
    for (int s1=0; s1<NUM_SLICES; s1++){
        for (int s2=s1+1; s2<NUM_SLICES; s2++){
            int sum_max = INT_MIN;
            int core_max = -1;
            for (int core=0; core<NUM_CORES; core++){
                int sum = latency_table[core][s1] + latency_table[core][s2];
                if (sum > sum_max){
                    sum_max = sum;
                    core_max = core;
                }
            }
           // printf("core %2d,  slices %2d %2d,  sum_max %3d\n", core_max, s1, s2, sum_max);
            for (int core=0; core<NUM_CORES; core++){
                int sum = latency_table[core][s1] + latency_table[core][s2];
                if (sum == sum_max && core != core_max){
                    printf("======> core %2d %2d,  slices %2d %2d,  sum_max %3d\n", core, core_max, s1, s2, sum_max);
                }
            }
        }
    }
}

void check_bug(){
    printf("cores  slices  sum\n");
    for (int s1=0; s1<NUM_SLICES; s1++){
        for (int s2=s1+1; s2<NUM_SLICES; s2++){
            for (int core1=0; core1<NUM_CORES; core1++){
                int sum1  = latency_table[core1][s1] + latency_table[core1][s2];
                for (int core2=core1+1; core2<NUM_CORES; core2++){
                    int sum2 = latency_table[core2][s1] + latency_table[core2][s2];
                    if (sum1 == sum2){
                        printf("%2d %2d   %2d %2d   %3d\n", core1, core2, s1, s2, sum1);
                    }
               }
               
           }
       }
   }
   printf("no bug!\n");
}

int main(int argc, char **argv) {

    if(argc != 4){
        printf("usage: ./program slice1 slice2 find_min\n");
        exit(EXIT_FAILURE); 
    }
    // 测试示例
    int slice1 = atoi(argv[1]);;  // 切片编号1
    int slice2 = atoi(argv[2]);;  // 切片编号2
    bool find_min = atoi(argv[3]);;  // 是否查找最小时延
    check_min();
    check_max();
    int matched_core = match_core(slice1, slice2, find_min);

    if (matched_core != -1) {
        printf("slices: %d  %d\n", slice1, slice2);
        printf("Matched core: %d\n", matched_core);
        printf("Latency sum: %d\n", 
               latency_table[matched_core][slice1] + latency_table[matched_core][slice2]);
    } else {
        printf("No valid core found.\n");
    }

    return 0;
}
